package com.example.demo.controller;

import java.util.Map;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import com.example.demo.model.Employee;
import com.example.demo.model.Teas;
import com.example.demo.service.TeasService;

@Controller
@RequestMapping("tea")
public class TeasController {

		@GetMapping("test")
		public @ResponseBody String test() {
			return "卖茶叶";
		}
		
		@Resource
		private TeasService teasService;
		
		@GetMapping("all")
		public  Object findAll(Map<String, Object> map) {
			map.put("teas", teasService.findAll());
			return "teas/tea";
		}
		
		@GetMapping("delete")
		public Object delete(int id) {
			boolean r=teasService.delete(id);
			System.out.println(r);
			return "redirect:all";
		}
		
		@GetMapping("add")
		public String add(Teas emp,Map<String, Object> map) {
			teasService.add(emp);
			return "redirect:all";
		}
		
		@GetMapping("update_view")
		public String updateView(@RequestParam("eid") Integer eid,Map<String, Object> map) {
			map.put("tea", teasService.finById(eid));
			return "teas/update";
		}
		
		@PostMapping("update")
		public String update(Teas tea,Map<String, Object> map) {
			boolean r=teasService.update(tea);
			return "redirect:all";
		}
		
		@GetMapping("finById")
		public ModelAndView finById(@RequestParam("eid") Integer eid) {
			 Teas find = teasService.finById(eid);
			return new ModelAndView("teas/tea","find",find); 
		}
		
}
